#include "DATime.h"
#include <QDateTime>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPainter>
#include <QTimer>
#include <QDateTime>

DATime::DATime(QWidget *parent)
    : QWidget{parent}
{

    setObjectName("dat");
    Title.setParent(this);
    Title.setObjectName("LabelTitle");
    Title.setAlignment(Qt::AlignCenter);

    Time.setParent(this);
    Time.setObjectName("LabelTime");
    Time.setAlignment(Qt::AlignCenter);
    QTimer *timer = new QTimer(this);

    CountDownTime.setParent(this);
    CountDownTime.setObjectName("cdTime");
    CountDownTime.setAlignment(Qt::AlignCenter);
    QObject::connect(timer,&QTimer::timeout,[=]()
    {
#if 0
        QTime m_time;
        m_time.setHMS(0, 0, 0, 0);                                       //初始化数据，时 分 秒 毫秒
        QDateTime local(QDateTime::currentDateTime());

        QString StrTime = local.toString("yyyy年MM月dd日 dddd  hh:mm:ss");
        QDateTime end_time = QDateTime::fromString( local.toString("yyyy-MM-dd")+ " 18:00:00","yyyy-MM-dd hh:mm:ss");
        QString Htime = m_time.addSecs(local.secsTo(end_time)).toString("hh");//计算时间差(秒)，将时间差加入m_time，格式化输出
        QString Mtime = m_time.addSecs(local.secsTo(end_time)).toString("mm");//计算时间差(秒)，将时间差加入m_time，格式化输出
        QString Stime = m_time.addSecs(local.secsTo(end_time)).toString("ss");//计算时间差(秒)，将时间差加入m_time，格式化输出
        Time.setText(StrTime);
        CountDownTime.setText("距离下班还有 "+years+"小时"+months+"分"+days+"秒");
#endif
        updateTime();
    });
    timer->start(1000);

    QVBoxLayout *vb = new QVBoxLayout();
    vb->addWidget(&Title);
    vb->addWidget(&Time);
    vb->addWidget(&CountDownTime);
    vb->setSpacing(0);
    vb->setContentsMargins(0,0,0,0);//方便兼容qt6

    setLayout(vb);
}
void DATime::paintEvent(QPaintEvent *e)
{
    //创建绘画对象
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing); // 反锯齿;
    painter.setBrush(QBrush(QColor(255, 255, 255, 100)));
    painter.setPen(Qt::transparent);

    QRect rect = this->rect();
    rect.setWidth(rect.width()-1);
    rect.setHeight(rect.height()-1);
    painter.drawRoundedRect(rect,5,5);

//    QPen pen;
//    pen.setColor(Qt::red);
//    pen.setWidth(0);
//    painter.setPen(pen);
//    painter.drawRect(this->rect());
//    painter.end();
    QWidget::paintEvent(e);
}

void DATime::updateTime()
{
    const QDateTime time = QDateTime::currentDateTime();
//    const QDateTime time = QDateTime::fromString("2022-07-27 18:22:22","yyyy-MM-dd hh:mm:ss");
    QDateTime time1 = QDateTime::fromString(time.toString("yyyy-MM-dd")+ " 09:00:00", "yyyy-MM-dd hh:mm:ss");
    QDateTime time2 = QDateTime::fromString(time.toString("yyyy-MM-dd")+ " 18:00:00", "yyyy-MM-dd hh:mm:ss");
    qint64 secs;
    if( time < time1 )
    {
        secs = time.secsTo(time1);
    }
    else if((time >= time1) && (time < time2) )
    {
        secs = time.secsTo(time2);
    }
    else
    {
        secs = time2.secsTo(time);
    }

    QTime t(0,0,0);
    t = t.addSecs(secs);
    qint64 days = secs/60/60/24;
    QString sd;
    QString s;
    QString stip = "";
    if (days != 0) {
        sd += QString::number(days) + " 天";
    }
    if(t.toString("h") != "0") {
        sd += t.toString(" h 时 m 分 s 秒");
    }
    else {
        sd += t.toString(" m 分 s 秒");
    }

    if (time < time1){
        s.append(tr("距离上班还有"));
        stip = s + sd;
        CountDownTime.setStyleSheet("font:bold 12pt;");
    }
    else if((time >= time1) && (time < time2) ){
        s.append(tr("距离下班还有"));
        stip = s + sd;
        CountDownTime.setStyleSheet("font:bold 12pt;");
    }
    else {
        s.append(tr("<font color=\"yellow\">下班了，休息吧！</font>"));
        stip = s;
        CountDownTime.setStyleSheet("font:bold 16pt;");
    }

    Title.setText(time.toString("hh:mm:ss"));
    Time.setText(time.toString("yyyy年mm月dd日 dddd"));
    CountDownTime.setText(stip);
}
